//Modified By Reem Sharawy 20/3/2014
public class drv_ctlr 
{

    //Added By Ahmed Adel
    public String selectedTransferSubType {get;set;}


    Public Boolean show_NewVisa {get; set;}
    Public Boolean show_DateOfEntry {get; set;}
    Public Boolean Show_PortofEntry {get; set;}
    Public Health_Card_Renewal_Form__c Application {get; set;}
    Public Boolean show_VisaAmendment {get; set;}
    Public Boolean show_NewDesignation {get; set;}
    Public Boolean show_NewPassport {get; set;}
    Public Boolean show_LostPassport {get; set;}
    Public Boolean show_ChangeOfNationality {get; set;}
    Public Boolean show_Others {get; set;}
    Public Boolean show_Cancellation {get; set;}
    Public Boolean show_DateOfExit {get; set;}
    Public Boolean show_GovernmentOrFreeZone {get; set;}
    Public Boolean show_FatherHusband {get; set;}
    Public Boolean show_Transfer {get; set;}
    
    Public String lastDateOfEntry {get; set;}
    Public String dateOfBirth {get; set;}
    Public String passportIssueDate {get; set;}
    Public String passportExpiryDate {get; set;}
    Public String SponserPassportExpiry {get; set;}
    Public String SponserResidencyExpiry {get; set;}
    Public String SponserDate {get; set;}
    Public String DateOfExit {get; set;}
    Public String DRVDate {get; set;}
    Public String DRVSponserPassportExpiry {get; set;}
    Public String DRVSponserResidencyExpiry {get; set;}
    
    public static Boolean testFlag=true;
    
    Public Static String WebParam {get; set;}
    public static string stdemo {get; set;}
    public static string url = null;
    
    public static string soapXML = null;
    public string getsoapXML(){return soapXML;}
    
    public static string xml = null;
    public string getxml() { return xml;}
    
    public void testFlagMark(){
       testFlag = false ;
    }
    
    public void Render_NewVisa()
    {
        if(Application.VRF_Visa_Type__c == 'New Visa')
        {
            show_NewVisa = true;
            show_Cancellation = false;
            show_NewDesignation = false;
            show_DateOfExit = false;
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false;
            show_VisaAmendment = false;
            //show_GovernmentOrFreeZone = false;
            //show_FatherHusband = false;         
            //Application.VRF_Transfer_Flag__c = false; 
            //show_Transfer = false;
        }
        else if(Application.VRF_Visa_Type__c == 'Renewal Visa')
        {
            show_NewVisa = false;      
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_VisaAmendment = false;
            show_Cancellation = false;
            show_NewDesignation = false;
            show_DateOfExit = false;
            show_DateOfEntry = false;
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false;
            show_Transfer = false;
         
            system.debug( 'Render NewVisa : ' + show_VisaAmendment );
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation')
        {
            show_NewVisa = false;
            show_Cancellation = true;
            show_DateOfExit = false;
            show_DateOfEntry = false;
            show_VisaAmendment = false;
            show_NewVisa = false;    
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_NewDesignation = false;      
            show_NewPassport = false; 
            show_ChangeOfNationality = false; 
            show_Others =  false; 
            show_Transfer = false;
        }        
        else if(Application.VRF_Visa_Type__c == 'Visa Amendment' )
        {
            show_VisaAmendment = true;
            show_Cancellation = false;
            show_NewVisa = false;   
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            Application.VRF_Transfer_Flag__c = false; 
            show_NewDesignation = false;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
            show_DateOfExit = false;
            show_Transfer = false;
            
            system.debug( 'Render NewVisa : ' + show_VisaAmendment );
        }
    }
    
    public void Render_Individual()
    {
        if(Application.VRF_Available_In__c == 'Individual currently inside UAE')
        {
            
            //show_DateOfEntry = true;
            //show_PortofEntry = true;
            show_DateOfExit = false;
            show_PortofEntry = false;
            Application.VRF_Transfer_Flag__c = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;     
            show_Transfer = false;    
            system.debug('reem inside');
        }
        else if(Application.VRF_Available_In__c == 'Transfer of visa' || Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' || Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual')
        {
            show_Transfer = true;
            Application.VRF_Transfer_Flag__c = true;
            show_PortofEntry = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;  
            system.debug('reem transfer');
        }
        else if(Application.VRF_Available_In__c == 'New Born Baby')
        {
            show_Transfer = false;
            show_PortofEntry = false;
            Application.VRF_Transfer_Flag__c = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false; 
            system.debug('reem new born');
        }
        else if(Application.VRF_Available_In__c == 'Individual currently outside UAE' && Application.VRF_Visa_Type__c == 'New Visa')
        {
            Application.VRF_Available_In__c = 'Individual currently outside UAE';
            show_PortofEntry = true;
            show_DateOfExit = false;
            Application.VRF_Transfer_Flag__c = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            show_Transfer = false; 
            
        system.debug('reem outside'); 
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation' && Application.VRF_Available_In__c == 'individual currently outside UAE')
        {
            show_DateOfExit = true;
            show_NewDesignation = false;
            Application.VRF_Transfer_Flag__c = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            show_Transfer = false; 
            system.debug('cancellation');
        }  
        system.debug('Reem : ' + Application.VRF_Visa_Type__c + Application.VRF_Available_In__c); 
        system.debug('Reem gov: ' +  show_GovernmentOrFreeZone);
        system.debug('Reem father: ' +  show_FatherHusband);
        system.debug('Reem transfer: ' +  show_Transfer );
        system.debug('Reem transferFlag: ' +  Application.VRF_Transfer_Flag__c);
    }
    public void Render_NewDesignation()
    {
        //system.debug('The Request Type Selected : ' + Application.request_type__c);
        if(Application.VRF_Visa_Type__c == 'Visa Amendment' && Application.request_type__c == 'New Designation' )
        {
            show_NewDesignation = true;
            show_NewPassport = false;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        
        else if(Application.request_type__c == 'New Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Lost Passport' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' &&  Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_NewDesignation = false;
            show_NewPassport = true;
            show_ChangeOfNationality = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Change Of Nationality' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Others' && Application.request_type__c != 'New Passport')
        {
            show_ChangeOfNationality = true;
            show_NewDesignation = false;
            show_NewPassport = false;
            show_Others = false;
        }
        else if(Application.request_type__c == 'Others' && Application.request_type__c != 'New Designation' && Application.request_type__c != 'Change Of Nationality' && Application.request_type__c != 'New Passport')
        {
            show_Others = true;
            show_ChangeOfNationality = false;
            show_NewDesignation = false;
            show_NewPassport = false;
        }
        
    }
    public void Render_Transfer()
    {
        //if(Application.VRF_Transfer_Flag__c == true)
        if(Application.VRF_Available_In__c == 'Transfer of visa' || Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' || Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual')
        {
            show_Transfer = true;
            Application.VRF_Transfer_Flag__c = true;
        }
        else
        {
            show_Transfer = true; 
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;   
            show_NewVisa = true;  
        }     
    }        
    public void Render_GovernmentOrFreeZone()
     {
        if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Transfer_Flag__c == true && selectedTransferSubType == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && selectedTransferSubType != 'Individual is currently sponsored by father/husband/another individual')
        //if(Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' && Application.VRF_Available_In__c != 'Individual is currently sponsored by father/husband/another individual')
        
        {   system.debug('Reem : ' + Application.VRF_Visa_Type__c );
            show_Transfer = true;
            show_GovernmentOrFreeZone = true;
            show_FatherHusband = false;
            Application.Sponsor_Type__c = 'Establishment' ;
            show_VisaAmendment = false;
            show_NewDesignation = false;
            Application.VRF_Transfer_Flag__c = true;
            //Application.VRF_Available_In__c = 'Transfer of visa';
        }
        else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Transfer_Flag__c == true && selectedTransferSubType == 'Individual is currently sponsored by father/husband/another individual' && selectedTransferSubType != 'Individual is currently sponsored by Dubai Government Entity or a Free zone')
        //else if(Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' && Application.VRF_Available_In__c != 'Individual is currently sponsored by Dubai Government Entity or a Free zone')
        
        {
            show_Transfer = true;
            show_FatherHusband = true;
            Application.Sponsor_Type__c = 'Resident' ;    
            show_GovernmentOrFreeZone = false;
            show_NewDesignation = false;
            show_VisaAmendment = false;
            Application.VRF_Transfer_Flag__c = true;           
            //Application.VRF_Available_In__c = 'Transfer of visa';
        }
        else if(Application.VRF_Transfer_Flag__c != true || Application.VRF_Transfer_Flag__c != true)
        {
            Application.VRF_Transfer_Flag__c = false;
            show_GovernmentOrFreeZone = false;
            show_FatherHusband = false;         
            show_Transfer = false; 
        }
    } 
    public drv_ctlr()
    {
        selectedTransferSubType = '' ;
        Application = new Health_Card_Renewal_Form__c();
        Application.First_Name__c = 'First';
        Application.Middle_Name__c = 'Middle';
        Application.Last_Name__c = 'Last';
        show_NewVisa = true;
        show_DateOfEntry = false;
    }
    
    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Country__c> CountriesList = [SELECT id, Name FROM Country__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Country__c country : CountriesList)
        {   system.debug('AAAAAAT : ' + country.id);
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getCountriesList()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<CountriesList__c> CountriesList = [SELECT id, Name FROM CountriesList__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(CountriesList__c country : CountriesList)
        {
            options.add(new SelectOption(country.id, country.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getCompanies()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Account> CompaniesList = [SELECT id, Name FROM Account Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Account company : CompaniesList)
        {
            options.add(new SelectOption(company.id, company.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getJobTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Occupation__c> OccupationsList = [SELECT id, Name FROM Occupation__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Occupation__c occupation : OccupationsList)
        {
            options.add(new SelectOption(occupation.id, occupation.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getQualifications()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Qualification__c> QualificationsList = [SELECT id, Name FROM Qualification__c WHERE IsActive__c = true Order By Name LIMIT 999];
        options.add(new SelectOption('', ''));
        for(Qualification__c qualification : QualificationsList)
        {
            options.add(new SelectOption(qualification.id, qualification.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getGenders()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Male', 'Male'));
        options.add(new SelectOption('Female', 'Female'));
        
        return options;
    }
    public List<SelectOption> getRelationSponser()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));     
        options.add(new SelectOption('Brother', 'Brother'));
        options.add(new SelectOption('Daughter', 'Daughter'));
        options.add(new SelectOption('Father', 'Father'));
        options.add(new SelectOption('Husband', 'Husband'));
        options.add(new SelectOption('Mom Husband', 'Mom Husband'));
        options.add(new SelectOption('Mother', 'Mother'));
        options.add(new SelectOption('Sister', 'Sister'));
        options.add(new SelectOption('Son', 'Son'));
        options.add(new SelectOption('Unrelated', 'Unrelated'));
        options.add(new SelectOption('Wife', 'Wife'));
        
        return options;
    }
    public List<SelectOption> getSponsorType()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Resident', 'Resident'));
        options.add(new SelectOption('Establishment', 'Establishment'));  
       
        return options;
    } 
    public List<SelectOption> getSponsorDepartment()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('ABU DHABI', 'ABU DHABI'));
        options.add(new SelectOption('AJMAN', 'AJMAN'));
        options.add(new SelectOption('ALFUJERAH', 'ALFUJERAH'));
        options.add(new SelectOption('DUBAI', 'DUBAI'));
        options.add(new SelectOption('RAS AL KHAIMAH', 'RAS AL KHAIMAH'));
        options.add(new SelectOption('SHARJAH', 'SHARJAH'));
        options.add(new SelectOption('UMM AL QUWAIN', 'UMM AL QUWAIN'));
        
        return options;
    }
        public List<SelectOption> getrelation()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Brother', 'Brother'));
        options.add(new SelectOption('Daughter', 'Daughter'));
        options.add(new SelectOption('Father', 'Father'));
        options.add(new SelectOption('Husband', 'Husband'));
        options.add(new SelectOption('Mom Husband', 'Mom Husband'));
        options.add(new SelectOption('Mother', 'Mother'));
        options.add(new SelectOption('Sister', 'Sister'));
        options.add(new SelectOption('Son', 'Son'));
        options.add(new SelectOption('Unrelated', 'Unrelated'));
        options.add(new SelectOption('Wife', 'Wife'));
        
        return options;
    }
    public List<SelectOption> getEstablishmentType()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Government', 'Government'));
        options.add(new SelectOption('Free Zone', 'Free Zone'));       
        
        return options;
    }
    public List<SelectOption> getTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Mr', 'Mr'));
        options.add(new SelectOption('Mrs', 'Mrs'));
        options.add(new SelectOption('Ms', 'Ms'));
        
        return options;
    }
    
    public List<SelectOption> getVRFVisaType()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('New Visa', 'New Visa'));
        options.add(new SelectOption('Renewal Visa', 'Renewal Visa'));
        options.add(new SelectOption('Visa Amendment', 'Visa Amendment'));
        options.add(new SelectOption('Visa Cancellation', 'Visa Cancellation'));
        
        return options;
    }
    public List<SelectOption> getVRFAvailableIn()
    {
        List<SelectOption> options = new List<SelectOption>();
        if(Application.VRF_Visa_Type__c == 'New Visa'){
            options.add(new SelectOption('Individual currently inside UAE', 'Individual currently inside UAE'));
            options.add(new SelectOption('Individual currently outside UAE', 'Individual currently outside UAE'));  
            options.add(new SelectOption('Transfer of visa', 'Transfer of visa')); 
            options.add(new SelectOption('New Born Baby', 'New Born Baby'));
            return options;
        }
        else if(Application.VRF_Visa_Type__c == 'Visa Cancellation'){
            options.add(new SelectOption('individual currently outside UAE', 'individual currently outside UAE'));
            options.add(new SelectOption('individual currently inside UAE', 'individual currently inside UAE'));
            return options;
        }
        return options;
    }
    public List<SelectOption> getVRFFields()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Individual is currently sponsored by father/husband/another individual', ' Individual is currently sponsored by father/husband/another individual'));
        options.add(new SelectOption('Individual is currently sponsored by Dubai Government Entity or a Free zone', 'Individual  is currently sponsored by Dubai Government Entity or a Free zone'));             
        return options;
    }
    public List<SelectOption> getRequestTypes()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('New Passport', 'New Passport'));
        //options.add(new SelectOption('New Designation', 'New Designation'));
        options.add(new SelectOption('Lost Passport', 'Lost Passport'));
        options.add(new SelectOption('Change Of Nationality', 'Change Of Nationality'));
        options.add(new SelectOption('Others', 'Others'));
        
        return options;
    }
    public List<SelectOption> getHoldUAEPermit()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Yes', 'Yes'));
        options.add(new SelectOption('No', 'No'));
        
        return options;
    }
    
    public List<SelectOption> getMaritalStatuses()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Single', 'Single'));
        options.add(new SelectOption('Married', 'Married'));
        options.add(new SelectOption('Divorced', 'Divorced'));
        options.add(new SelectOption('Widowed', 'Widowed'));
        
        return options;
    }
       public List<SelectOption> getEmirates()
    { 
        List<SelectOption> options = new List<SelectOption>();
        
        List<Emirates__c> EmiratesList = [SELECT id, Name FROM Emirates__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(Emirates__c Emirates : EmiratesList)
        {
            options.add(new SelectOption(Emirates.id, Emirates.name));
            }    
        return options;
    }
    public List<SelectOption> getCity()
    {
    
        List<SelectOption> options = new List<SelectOption>();
        
        List<City__c> CityList = [SELECT id, Name FROM City__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(City__c City : CityList)
        {
            options.add(new SelectOption(City.id, City.name));     
        }
        
        return options;
    }
 
    public List<SelectOption> getArea()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Area__c> AreaList = [SELECT id, Name FROM Area__c WHERE Active__c = true Order By Name LIMIT 1000];
        options.add(new SelectOption('', ''));
        for(Area__c Area : AreaList)
        {
            options.add(new SelectOption(Area.id, Area.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getReligions()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Bahaei', 'Bahaei'));
        options.add(new SelectOption('Budhist', 'Budhist'));
        options.add(new SelectOption('Cristian', 'Cristian'));
        options.add(new SelectOption('Hindu', 'Hindu'));
        options.add(new SelectOption('Kadiani', 'Kadiani'));
        options.add(new SelectOption('Muslim', 'Muslim'));
        options.add(new SelectOption('Sikh', 'Sikh'));
        options.add(new SelectOption('Others', 'Others'));
        
        return options;
    }
    public static Date stringToDate(String s){
      //XML Date String is in the format DD-MM-YYYY
      try{
          String[] stringDate = s.split('/');
          Integer d =  Integer.valueOf(stringDate[0]);
          Integer m = Integer.valueOf(stringDate[1]);
          Integer y = Integer.valueOf(stringDate[2]);
          
          if ( d > 31 || m > 12 || y < 1800){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MM/YYYY format.'));
             return null ;
          }
          
          return date.newInstance(y,m,d);
      }catch(System.Exception er){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MM/YYYY format.'));
          return null ;
      }    
    }
    
    Public PageReference CreateApplication()
    {   
        
        if (Application.VRF_Visa_Type__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose New Visa or Renewal Visa'));       
        }else{
           if (Application.VRF_Visa_Type__c == 'New Visa'){
              if (Application.VRF_Available_In__c == null){
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Availability'));
              }
           }
         
        }
        if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Transfer_Flag__c == true && selectedTransferSubType == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' )
        {
            if(Application.Sponsor_Type__c == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must choose sponsor type in the current sponsor details part'));
            }
            if(Application.Establishment_Number__c == null || Application.Establishment_Number__c ==  ''){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter Establishment Number in the current sponsor details part'));
            }
            if (Application.Commercial_Activity__c == null || Application.Commercial_Activity__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Commercial Activity in the current sponsor details part'));
            }
            if (Application.Sponsor_Emirates__c == null || Application.Sponsor_Emirates__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Emirates in the current sponsor details part'));
            }
            if (Application.Establishment_Type__c == null || Application.Establishment_Type__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Establishment Type in the current sponsor details part'));
            }
            if (Application.Sponsor_Address__c == null || Application.Sponsor_Address__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Address in the current sponsor details part'));
            }
            if (Application.Transfer_Emirates__c == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Emirate in the current sponsor details part'));
            }
            if (Application.Transfer_City__c == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for City in the current sponsor details part'));
            }
            if (Application.Residency_Tel__c == null || Application.Residency_Tel__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Telephone in the current sponsor details part'));
            }
            if (Application.Sponser_Office_Phone__c== null || Application.Sponser_Office_Phone__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Office Telephone in the current sponsor details part'));
            }
            if (Application.Sponsor_P_O_Box__c == null || Application.Sponsor_P_O_Box__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for P.O.Box in the current sponsor details part'));
            }
            if (Application.Sponser_Mobile__c == null || Application.Sponser_Mobile__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mobile in the current sponsor details part'));
            }
            
        } 
        
        
        if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Transfer_Flag__c == true && selectedTransferSubType == 'Individual is currently sponsored by father/husband/another individual' )
        {
            if(Application.Sponsor_Type__c == null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must choose sponsor type in the current sponsor details part'));
            }
            if (Application.Sponsor_Department__c == null || Application.Sponsor_Department__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value Sponsor Department in the current sponsor details part'));
            }
            if (Application.Sponser_First_Name__c== null || Application.Sponser_First_Name__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for  English First Name in the current sponsor details part'));
            }
            if (Application.Sponser_Last_Name__c== null || Application.Sponser_Last_Name__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for English Last Name in the current sponsor details part'));
            }
            if (Application.Sponser_Nationality__c== null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Nationality in the current sponsor details part'));
            }
            if (Application.Sponsor_Gender__c == null || Application.Sponsor_Gender__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Gender in the current sponsor details part'));
            }
            if (SponserDate == null || SponserDate ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date of Birth in the current sponsor details part'));     
            } else
            {
                Application.Sponser_Date__c= stringToDate(SponserDate);
            }       
            if (Application.Residency_Years__c == null || Application.Residency_Years__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Years in the current sponsor details part'));
            }
            if (SponserResidencyExpiry == null || SponserResidencyExpiry ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Residency expiry date in the current sponsor details part'));
            }else 
            {
                Application.Sponser_Residency_Expiry__c= stringToDate(SponserResidencyExpiry);
            }
            if (Application.Sponser_Passport_Number__c== null || Application.Sponser_Passport_Number__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Passport Number in the current sponsor details part'));
            }
            if (SponserPassportExpiry == null || SponserPassportExpiry ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a value for expiry date in the current sponsor details part'));
           } else 
           {
                Application.Sponser_Passport_Expiry__c= stringToDate(SponserPassportExpiry);
           }
           if (Application.Sponsor_Mother_Full_Name__c == null || Application.Sponsor_Mother_Full_Name__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mother Full Name in the current sponsor details part'));
           }          
           if (Application.Sponsor_Address__c == null || Application.Sponsor_Address__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponsor Address in the current sponsor details part'));
           }
           if (Application.Transfer_Emirates__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Emirate in the current sponsor details part'));
           }
           if (Application.Transfer_City__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for City in the current sponsor details part'));
           }
           if (Application.Residency_Tel__c == null || Application.Residency_Tel__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Residency Telephone in the current sponsor details part'));
           }
           if (Application.Sponser_Office_Phone__c== null || Application.Sponser_Office_Phone__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Office Telephone in the current sponsor details part'));
           }
           if (Application.Sponsor_P_O_Box__c == null || Application.Sponsor_P_O_Box__c ==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for P.O.Box in the current sponsor details part'));
           }
           if (Application.Sponser_Mobile__c== null || Application.Sponser_Mobile__c==  ''){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Mobile in the current sponsor details part'));
           }
           if (SponserPassportExpiry == null || SponserPassportExpiry ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Passport Expiry'));
           }else{
           Application.DRV_Sponser_Passport_Expiry__c = stringToDate(DRVSponserPassportExpiry);
           }
             if (SponserResidencyExpiry == null || SponserResidencyExpiry ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Residency Expiry'));
           }else{
           Application.DRV_Sponser_Residency_Expiry__c = stringToDate(DRVSponserResidencyExpiry);
           }
         
        }
        
        /*if (Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual currently outside UAE') //|| (Application.VRF_Available_In__c != 'Individual currently inside UAE' && Application.VRF_Visa_Type__c != 'Renewal Visa'&& Application.VRF_Available_In__c != 'Transfer of visa' && Application.VRF_Available_In__c != 'New Born Baby'))
            {
                if (Application.Port_of_Entry__c == null ){
               ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Port of Entry'));
             }
            }*/
        
        if (Application.Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Title'));
        }
        
        if (Application.First_Name__c == 'First' || Application.First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for First Name'));
        }
        if (Application.Middle_Name__c == 'Middle'){          
           Application.Middle_Name__c = '' ;
        }
        if (Application.Last_Name__c == 'Last' || Application.Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Last Name'));
        }
        if (Application.Currently_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Currently Nationality'));
        }
        if (Application.Previous_Nationality__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Previous Nationality'));
        }
        
        if (dateOfBirth== null || dateOfBirth==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Date of Birth'));
        }else{
           Application.Date_of_Birth__c = stringToDate(dateOfBirth);        
        }
        
        if (Application.Country_of_Birth__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Country of Birth'));
        }
        
        if (Application.City_of_Birth__c == null || Application.City_of_Birth__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose City of Birth'));
        }
        
        if (Application.Passport_Number__c == null || Application.Passport_Number__c == '' ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
        }
        
        if (passportIssueDate == null || passportIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Issue Date'));
        }else{
           Application.Passport_Issue_Date__c = stringToDate(passportIssueDate);         
        }
        
        if (passportExpiryDate == null || passportExpiryDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Expiry Date'));
        }else{
           Application.Passport_Expiry_Date__c = stringToDate(passportExpiryDate);
           
        }
        
        if (Application.Passport_Country_of_Issue__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Country of Issue'));
        }
        
        if (Application.Passport_City_of_Issue__c == null || Application.Passport_City_of_Issue__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport City of Issue'));
        }
        
        if (Application.Mother_Full_Name__c == null || Application.Mother_Full_Name__c ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Mother Full Name'));
        }
        
        if (Application.Marital_Status__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Marital Status'));
        }
        
        if (Application.Religion__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Religion'));
        }
        
        if (Application.Job_Title_Position__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Job Title/Position'));
        }
        
        if (Application.DRV_Relation_with_Sponser__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Relation With Sponser'));
        }
        
        if (Application.Gender__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Gender'));
        }
        If(Application.VRF_Available_In__c == 'Transfer of visa' )
        {
             Application.VRF_Available_In__c = selectedTransferSubType ;
        }
        if (Application.VRF_Visa_Type__c == 'Visa Cancellation' && Application.VRF_Available_In__c == 'individual currently outside UAE' && (DateOfExit !=  '' && DateOfExit != null) ) 
        {
           Application.Date_of_exit__c = stringToDate(DateOfExit);
        }
        
           /*if (SponserDate == null || SponserDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose SponserCompany Date'));
           }else{
           Application.DRV_Sponser_Date__c = stringToDate(DRVDate);
           } */
        
        // Validate Sponser Section
        if (Application.DRV_Sponser_Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Title'));
        }
        else
        {
                If(Application.DRV_Sponser_Title__c == 'Mr')
                {
                        Application.Sponsor_Gender__c = 'Male';
                }
                else
                {
                        Application.Sponsor_Gender__c = 'Female';
                }
                
        }    
        if (Application.DRV_Sponser_First_Name__c == 'First' || Application.DRV_Sponser_First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser First Name'));
        }
        if (Application.DRV_Sponser_Middle_Name__c == 'Middle'){          
           Application.DRV_Sponser_Middle_Name__c = '' ;
        }
        if (Application.DRV_Sponser_Last_Name__c == 'Last' || Application.DRV_Sponser_Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Last Name'));
        }
        
        if (Application.DRV_Sponser_Currently_Nationality__c == null ){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Currently Nationality'));
        }
        
        if (Application.DRV_Sponser_Home_Phone__c == null || Application.DRV_Sponser_Home_Phone__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Home Phone'));
        }
        if (Application.DRV_Sponser_Office_Phone__c == null || Application.DRV_Sponser_Office_Phone__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Office Phone'));
        }        
        if (Application.DRV_Sponser_Visa_Number_1__c == '' ){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Visa Number Part 1'));
        }
        
        if (Application.DRV_Sponser_Visa_Number_2__c == '' ){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Visa Number Part 2'));
        }
        
        if (Application.DRV_Sponser_Visa_Number_3__c == '' ){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Visa Number Part 3'));
        }
        
        if (Application.DRV_Sponser_Passport_Number__c == null || Application.DRV_Sponser_Passport_Number__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Sponser Passport Number'));
        }
        
       
        
/*       if (Application.DRV_Sponser_Company_Name__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Company'));
        }
*/
        if (DRVSponserPassportExpiry == null || DRVSponserPassportExpiry ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Passport Expiry'));
        }else{
           Application.DRV_Sponser_Passport_Expiry__c = stringToDate(DRVSponserPassportExpiry);
        }
        
        if (DRVSponserResidencyExpiry == null || DRVSponserResidencyExpiry ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Residency Expiry'));
        }else{
           Application.DRV_Sponser_Residency_Expiry__c = stringToDate(DRVSponserResidencyExpiry);
        }
        
        if (Application.DRV_Sponser_Company_Name__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Sponser Company'));
        }
        
        if (DRVDate == null || DRVDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose SponserCompany Date'));
        }else{
           Application.DRV_Sponser_Date__c = stringToDate(DRVDate);
        }

        
        if (ApexPages.hasMessages()){
        Integer msgSize = ApexPages.getMessages().size() ;
        for(Integer i = 0 ; i < msgSize ; i++){
            system.debug('AAT : ' + i + ' :: ' + ApexPages.getMessages().get(i).getDetail());
        }
          
           return null ;
        }
        
        
        /*//To be Removed Start
        Application.Date_of_Birth__c = stringToDate(dateOfBirth);
           Application.Passport_Issue_Date__c = stringToDate(passportIssueDate);
           Application.Passport_Expiry_Date__c = stringToDate(passportExpiryDate);
           If(Application.DRV_Sponser_Title__c == 'Mr')
                {
                        Application.Sponsor_Gender__c = 'Male';
                }
                else
                {
                        Application.Sponsor_Gender__c = 'Female';
                }
                Application.DRV_Sponser_Passport_Expiry__c = stringToDate(SponserPassportExpiry);
           Application.DRV_Sponser_Date__c = stringToDate(SponserDate);
           Application.DRV_Sponser_Residency_Expiry__c = stringToDate(SponserResidencyExpiry);
        //To Be Removed End */
        
        //Added By Ahmed Adel on 3 Oct 2013
        //Below Code will Fill Map with Application Fields Api Name With Type and another Map for Api Name with Field Value from User
        //Map<String , String> appApiFldValueMap = new Map<String , String>();
        //Map<String , String> appApiFldTypeMap = new Map<String , String>();        
        Map<String, Schema.SObjectField> M = Schema.SObjectType.Health_Card_Renewal_Form__c.fields.getMap();      
        //for (String item : M.keySet()){
        //     appApiFldTypeMap.put(item , String.ValueOf(m.get(item).getDescribe().getType()));
        //     //appApiFldValueMap.put(item , String.valueOf(Application.get(item)));
        //     system.debug('ADS : item : ' + item + '  , type : '  + appApiFldTypeMap.get(item) + '  , Value : ' + Application.get(item));
        //}
        
        
       
        try
        {
                Form_Header__c Header;
                system.debug('AAT Application.VRF_Visa_Type__c :: ' + Application.VRF_Visa_Type__c);
                if(Application.VRF_Visa_Type__c == 'Renewal Visa')
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Renewal'];
                }
                else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual currently inside UAE')
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Inside UAE - Visa Stamping'];
                }
                else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual currently outside UAE')
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Outside UAE - Visa Stamping'];
                }
                /*else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Transfer of visa')
                {
                        //PO Commented Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Inside UAE - Visa Stamping'];
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer'];


                }*/
                else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'New Born Baby')
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Inside UAE - Visa Stamping'];
                }
                else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual' )
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer'];
                }
                else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone' )
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer'];
                }
                else if(Application.VRF_Visa_Type__c == 'Visa Amendment' )
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Amendment and Cancellation'];
                }
                else if(Application.VRF_Visa_Type__c == 'Visa Cancellation' )
                {
                        Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Cancellation'];
                }
            
            String Webservice_String = '';
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                //Below 4 Lines are Commentes as the API name is Required Field and Can not be Empty
                //if(Detail.Field_Master__r.API_Name__c == NULL)
                //{
                //    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                //}else
                
                     if(Detail.Source__c == 'Form')
                    {     
                       String FieldValue = '';
                       
                       //STRING REFERENCE BOOLEAN DATE DATETIME PICKLIST PHONE TEXTAREA EMAIL DOUBLE ID URL
                       String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                       string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                       if(Strg == ''){//Means Field is not lookup or Master Relationship                         
                         if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                             FieldValue = dayField.left(2) + '/' + string.valueOf(strFieldValue).split('-', 0)[1] + '/' + string.valueOf(strFieldValue).split('-', 0)[0];
                         }else{
                             FieldValue = strFieldValue ;
                         }
                         
                         if(FieldValue != null){
                            if(Detail.Field_Master__r.API_Name__c == 'Gender__c'){
                               //PO Commented if(Header.Name == 'Non DIFC Sponsorship Form - New - Inside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Outside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Transfer of visa'){
                               if(Header.Name == 'Non DIFC Sponsorship Form - New - Inside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Outside UAE' || Header.Name == 'Non DIFC Sponsorship Form - Transfer'){

                                  if(FieldValue == 'Female') {
                                     Webservice_String += FieldValue + '*6|';
                                  }else{
                                     Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                  }
                                }else{
                                     Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                }
                            }else{
                                Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                            }   
                         }else{Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                         
                       }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
        system.debug( 'reemBarcode : ' + Detail.Field_Master__r.API_Name__c);
        system.debug( 'reemBarcode2 : ' + Detail);
        if(Application.get(Detail.Field_Master__r.API_Name__c) != null)
        {
                    tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                    try
                    {
                        Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                    }catch(System.Exception er)
                    { 
                        Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                    }
                            
         }
                      /*      tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            try
                            {
                              Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }*/
                       }
                        
                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }
            }    
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo1 = stdemo;
            
            
            //Added by Reem Sharawy 'Non DIFC Sponsorship Form - Transfer'
                       // String Webservice_String = '';
          for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                //Below 4 Lines are Commentes as the API name is Required Field and Can not be Empty
                //if(Detail.Field_Master__r.API_Name__c == NULL)
                //{
                //    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                //}else
                
                     if(Detail.Source__c == 'Form')
                    {     
                       String FieldValue = '';
                       
                       //STRING REFERENCE BOOLEAN DATE DATETIME PICKLIST PHONE TEXTAREA EMAIL DOUBLE ID URL
                       String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                       string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                       if(Strg == ''){//Means Field is not lookup or Master Relationship                         
                         if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                             FieldValue = dayField.left(2) + '/' + string.valueOf(strFieldValue).split('-', 0)[1] + '/' + string.valueOf(strFieldValue).split('-', 0)[0];
                         }else{
                             FieldValue = strFieldValue ;
                         }
                         
                         if(FieldValue != null){
                            if(Detail.Field_Master__r.API_Name__c == 'Gender__c'){
                               //PO Commented if(Header.Name == 'Non DIFC Sponsorship Form - New - Inside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Outside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Transfer of visa'){
                               if(Header.Name == 'Non DIFC Sponsorship Form - Transfer'){

                                  if(FieldValue == 'Female') {
                                     Webservice_String += FieldValue + '*6|';
                                  }else{
                                     Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                  }
                                }else{
                                     Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                }
                            }else{
                                Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                            }   
                         }else{Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                         
                       }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
        system.debug( 'reemBarcode : ' + Detail.Field_Master__r.API_Name__c);
        system.debug( 'reemBarcode2 : ' + Detail);
        if(Application.get(Detail.Field_Master__r.API_Name__c) != null)
        {
                    tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                    try
                    {
                        Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                    }catch(System.Exception er)
                    { 
                        Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                    }
                            
         }
           
                       }
                        
                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }
            }    
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo6 = stdemo;
            
            
            
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'SAP3'];
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                if(Detail.Source__c == 'Form')
                    {
                        //Default field value is blank
                        String FieldValue = '';
                        //Sets the application field value
                        String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                        //Retreives reference to value if present
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        //If reference to is blank
                        if(Strg == ''){
                            if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                              FieldValue = dayField.left(2) + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[1] + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[0];
                             
                            }else{
                             FieldValue = strFieldValue ;
                            }
                             //Added by Paulo Oct 21, 2013 - pass the fieldvalue to the webstring
                             Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
                            
                            String selectedField = Detail.Field_Master__r.Referencing_Value__c ;
                            if (Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c') selectedField = 'SAP_Name__c' ;
                            
                            tagrcd = Database.Query('SELECT id, ' + selectedField + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }

                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo3 = stdemo;
            
            //Added by Reem Sharawy SAP1
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'SAP1'];
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                if(Detail.Source__c == 'Form')
                    {
                        //Default field value is blank
                        String FieldValue = '';
                        //Sets the application field value
                        String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                        //Retreives reference to value if present
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        //If reference to is blank
                        if(Strg == ''){
                            if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                              FieldValue = dayField.left(2) + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[1] + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[0];
                             
                            }else{
                             FieldValue = strFieldValue ;
                            }
                             //Added by Paulo Oct 21, 2013 - pass the fieldvalue to the webstring
                             Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
                            
                            String selectedField = Detail.Field_Master__r.Referencing_Value__c ;
                            if (Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c') selectedField = 'SAP_Name__c' ;
                            
                            tagrcd = Database.Query('SELECT id, ' + selectedField + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }

                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo4 = stdemo;
            
            
            //Added by Reem Sharawy SAP2
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'SAP2'];
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                if(Detail.Source__c == 'Form')
                    {
                        //Default field value is blank
                        String FieldValue = '';
                        //Sets the application field value
                        String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                        //Retreives reference to value if present
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        //If reference to is blank
                        if(Strg == ''){
                            if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                              FieldValue = dayField.left(2) + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[1] + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[0];
                             
                            }else{
                             FieldValue = strFieldValue ;
                            }
                             //Added by Paulo Oct 21, 2013 - pass the fieldvalue to the webstring
                             Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
                            
                            String selectedField = Detail.Field_Master__r.Referencing_Value__c ;
                            if (Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c') selectedField = 'SAP_Name__c' ;
                            
                            tagrcd = Database.Query('SELECT id, ' + selectedField + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }

                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string sap2 = stdemo;
            
            
           /* //Transfer QR added by Reem Sharawy
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer - Father'];
                
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                if(Detail.Source__c == 'Form')
                    {
                        //Default field value is blank
                        String FieldValue = '';
                        //Sets the application field value
                        String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                        //Retreives reference to value if present
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        //If reference to is blank
                        if(Strg == ''){
                            if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                              FieldValue = dayField.left(2) + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[1] + '.' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[0];
                             
                            }else{
                             FieldValue = strFieldValue ;
                            }
                             //Added by Paulo Oct 21, 2013 - pass the fieldvalue to the webstring
                             Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
                            
                            String selectedField = Detail.Field_Master__r.Referencing_Value__c ;
                            if (Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c') selectedField = 'SAP_Name__c' ;
                            
                            tagrcd = Database.Query('SELECT id, ' + selectedField + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }

                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            
            Webservice_String += '|' + Header.Barcode_Type__c;
            
            WebParam = Webservice_String;
            GenerateBarcode();
            string tdemo6 = stdemo;  */
              
            
            
 //PO Additional QR Code 18032014 - Transfer 1 barcode
 string transferdemo  = null;
 if(Application.VRF_Visa_Type__c == 'New Visa' && (Application.VRF_Available_In__c == 'Transfer of visa' 
 || Application.VRF_Available_In__c == 'Individual is currently sponsored by Dubai Government Entity or a Free zone'
 || Application.VRF_Available_In__c == 'Individual is currently sponsored by father/husband/another individual')) {
                        if(selectedTransferSubType == 'Individual is currently sponsored by Dubai Government Entity or a Free zone')
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer - Gov'];
                        else
            Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer - Father'];
            Webservice_String = '';
            
            for(Form_Detail__c Detail : Header.Form_Details__r)
            {
                if(Detail.Field_Master__r.API_Name__c == NULL)
                {
                    Webservice_String += '*' + Detail.Tab_Space__c + '|';
                }
                if(Detail.Source__c == 'Form')
                    {
                        //Default field value is blank
                        String FieldValue = '';
                        //Sets the application field value
                        String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                        //Retreives reference to value if present
                        string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                        //If reference to is blank
                        if(Strg == ''){
                            if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                             //Set the day field to correct format
                              String dayField = strFieldValue.split('-', 0)[2]; 
                              FieldValue = dayField.left(2) + '/' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[1] + '/' + String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c)).split('-', 0)[0];
                             
                            }else{
                             FieldValue = strFieldValue ;
                            }
                             //Added by Paulo Oct 21, 2013 - pass the fieldvalue to the webstring
                             Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                        }else{ //Means That Field is Related to Lookup or Master Details Relationship
                            FieldValue = strFieldValue ;
                            Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                            Schema.SObjectType stype = gd.get(Strg);
                            sObject tagrcd;
                            tagrcd = stype.newSObject();
                            
                            String selectedField = Detail.Field_Master__r.Referencing_Value__c ;
                            if (Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c') selectedField = 'SAP_Name__c' ;
                            
                            tagrcd = Database.Query('SELECT id, ' + selectedField + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                            
                            try
                            { if(Detail.Field_Master__r.API_Name__c == 'Passport_Country_of_Issue__c' || Detail.Field_Master__r.API_Name__c == 'Country_of_Birth__c' || Detail.Field_Master__r.API_Name__c == 'Currently_Nationality__c'){Webservice_String += tagrcd.get('SAP_Name__c') + '*' + Detail.Tab_Space__c + '|';}
                              else {Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';}
                            }catch(System.Exception er)
                            { 
                              Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                            }
                        }

                    }
                    else if(Detail.Source__c == 'Constant')
                    {
                        if(Detail.Default_Value__r.Value__c != null){
                        Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                        else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                    }   
            }
            
            Webservice_String += '|' + Header.Barcode_Type__c;
            stdemo = '';
            WebParam = Webservice_String;
            GenerateBarcode();
            transferdemo = stdemo;
 }         
//PO End QR Code
 
              
 
            
            Boolean bm_flag = false;
            stdemo='';
            if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual currently inside UAE')
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Inside UAE'];
                bm_flag = true;
                //stdemo='';
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Individual currently outside UAE')
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Outside UAE'];
                bm_flag = true;
                stdemo='';
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'Transfer of visa')
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - Transfer'];
                bm_flag = true;
                stdemo='';
            }
            else if(Application.VRF_Visa_Type__c == 'New Visa' && Application.VRF_Available_In__c == 'New Born Baby')
            {
                Header = [SELECT id, Name, Barcode_Type__c, (SELECT id, Name, Field_Master__c, Field_Master__r.API_Name__c, Field_Master__r.Reference_To__c, Field_Master__r.Referencing_Value__c, Position__c, Tab_Space__c, Source__c, Default_Value__c, Default_Value__r.Value__c FROM Form_Details__r order by Position__c) From Form_Header__c WHERE Name = 'Non DIFC Sponsorship Form - New - Inside UAE'];
                bm_flag = true;
                stdemo='';
            }
            
            string tdemo2 = null;
            Webservice_String = '';
            if(bm_flag)
            {
                stdemo='';
                    for(Form_Detail__c Detail : Header.Form_Details__r)
                    {
                        if(Detail.Field_Master__r.API_Name__c == NULL)
                        {
                            Webservice_String += '*' + Detail.Tab_Space__c + '|';
                        }else if(Detail.Source__c == 'Form'){     
                               String FieldValue = '';
                               //STRING REFERENCE BOOLEAN DATE DATETIME PICKLIST PHONE TEXTAREA EMAIL DOUBLE ID URL
                               String strFieldValue = String.ValueOf(Application.get(Detail.Field_Master__r.API_Name__c));
                               string Strg = Detail.Field_Master__r.Reference_To__c.substring(1, Detail.Field_Master__r.Reference_To__c.length() - 1);
                               if(Strg == ''){//Means Field is not lookup or Master Relationship
                                   if (String.ValueOf(m.get(Detail.Field_Master__r.API_Name__c).getDescribe().getType()) == 'DATE' ){
                                    String dayField = strFieldValue.split('-', 0)[2];   
                                    FieldValue = dayField.left(2)  + '/' + string.valueOf(strFieldValue).split('-', 0)[1] + '/' + string.valueOf(strFieldValue).split('-', 0)[0];
                                   }else{
                                       FieldValue = strFieldValue ;
                                   }
                                   if(FieldValue != null)
                                    {
                                        if(Detail.Field_Master__r.API_Name__c == 'Gender__c')
                                        {
                                            if(Header.Name == 'Non DIFC Sponsorship Form - New - Inside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Outside UAE' || Header.Name == 'Non DIFC Sponsorship Form - New - Transfer of visa')
                                            {
                                                if(FieldValue == 'Female')
                                                {
                                                    Webservice_String += FieldValue + '*6|';
                                                }
                                                else
                                                {
                                                    Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                                }
                                            }
                                            else
                                            {
                                                Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                            }
                                        }
                                        else
                                        {
                                            Webservice_String += FieldValue + '*' + Detail.Tab_Space__c + '|';
                                        }   
                                    }else{Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                                   
                                   
                               }else{ //Means That Field is Related to Lookup or Master Details Relationship
                                   FieldValue = strFieldValue ;
                                   Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
                                   Schema.SObjectType stype = gd.get(Strg);
                                   sObject tagrcd;
                                   tagrcd = stype.newSObject();
                                   tagrcd = Database.Query('SELECT id, ' + Detail.Field_Master__r.Referencing_Value__c + ' FROM ' + Strg + ' WHERE id = \'' + Application.get(Detail.Field_Master__r.API_Name__c)+ '\'');
                                   try
                                    {
                                      Webservice_String += tagrcd.get(Detail.Field_Master__r.Referencing_Value__c) + '*' + Detail.Tab_Space__c + '|';
                                    }catch(System.Exception er)
                                    { 
                                      Webservice_String += '' +  + '*' + Detail.Tab_Space__c + '|'; 
                                    }
                               }
        
                               
                              
                            }
                            else if(Detail.Source__c == 'Constant')
                            {
                                if(Detail.Default_Value__r.Value__c != null){
                                Webservice_String += Detail.Default_Value__r.Value__c + '*' + Detail.Tab_Space__c + '|';}
                                else { Webservice_String += '*' + Detail.Tab_Space__c + '|';}
                            }
                    }
                    
                    Webservice_String += '|' + Header.Barcode_Type__c;
                    
                    WebParam = Webservice_String;
                    stdemo='';
                    GenerateBarcode();
                    tdemo2 = stdemo;
            }
            
            insert(Application);
            
            Barcode__c br = new Barcode__c(Image_URL__c = tdemo1, Application__c = Application.id);
            Insert(br);
            
            if(tdemo2 != null)
            {
                Barcode__c br2 = new Barcode__c(Image_URL__c = tdemo2, Application__c = Application.id);
                Insert(br2);
            }
            //PO Added
            if(tdemo4 != null)
            {
                Barcode__c br4 = new Barcode__c(Image_URL__c = tdemo4, Application__c = Application.id);
                Insert(br4);
            }
            if(sap2 != null) //SAP2
            {
                Barcode__c br4 = new Barcode__c(Image_URL__c = sap2, Application__c = Application.id);
                Insert(br4);
            }                        
            //PO Added
            
            if(transferdemo == null) //SAP3
            {
                Barcode__c br3 = new Barcode__c(Image_URL__c = tdemo3, Application__c = Application.id);
                Insert(br3);
            }
            
            //transfer1 qrcode
            if(transferdemo != null) //Transfer 1
            {
                Barcode__c transferbc = new Barcode__c(Image_URL__c = transferdemo, Application__c = Application.id);
                Insert(transferbc);
            }
            
            
            PageReference pg = new PageReference('/apex/drvPDF?id=' + Application.id);

            return pg;
        }
        catch(System.Exception Exp)
        {
            System.debug('Application Exception : '+ Exp.getMessage() + ' - ' + Exp.getLineNumber());
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, Exp.getMessage() + ' - ' + Exp.getLineNumber());
            ApexPages.AddMessage(myMsg);
            return null;
        }
    }
    
    Public PageReference ResetApplication()
    {
        Application = new Health_Card_Renewal_Form__c();
        lastDateOfEntry = '';
        dateOfBirth = '';
        passportIssueDate = '';
        passportExpiryDate = '';
        SponserPassportExpiry = '';
        SponserResidencyExpiry = '';
        SponserDate = '';
        DateOfExit = '';
        return null;
    }
    
    Public PageReference ToMainMenu()
    {
        PageReference pg = new PageReference('/home');
        return pg;
    }
    
    public List<SelectOption> getPortOfEntries()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('', ''));
        options.add(new SelectOption('Abu Dhabi International Airport', 'Abu Dhabi International Airport'));
        options.add(new SelectOption('Al Ain International Airport', 'Al Ain International Airport'));
        options.add(new SelectOption('Dubai International Airport', 'Dubai International Airport'));
        options.add(new SelectOption('Sharjah International Airport', 'Sharjah International Airport'));
        options.add(new SelectOption('Other', 'Other'));
        
        return options;
    }
    
    /*public static Date stringToDate(String s){
      //XML Date String is in the format dd/mm/yyyy
      String[] stringDate = s.split('-');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    } */  
    
    
    public static void Initializevariables()
    {
        // The webservice url -  Pass in the endpoint to be used using the string url 
        url = 'http://demo.smartpaperbpm.com/difc/soap_server/hellowsdl2.php';  

        soapXML = '<?xml version="1.0" encoding="ISO-8859-1"?>';
        soapXML += '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:servicename">';
        soapXML += '<SOAP-ENV:Body>';
        soapXML += '<DoSomething xmlns="urn:servicename">';
        soapXML += '<sf-parameters xmlns="">';
        soapXML += WebParam.replace(' & ', ' &amp; ') + '</sf-parameters>';
        soapXML += '</DoSomething>';
        soapXML += '</SOAP-ENV:Body>';
        soapXML += '</SOAP-ENV:Envelope>';
        
        system.debug('web params passed =>' + WebParam);
    }
    
    public static void GenerateBarcode()
    {    
        // Instantiate a new http object
        Http h = new Http();
              
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        
        Initializevariables();
        
        req.setBody(soapXML);
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader('Content-length', '510' );
        req.setHeader('Content-Type', 'text/xml; charset=ISO-8859-1');  
        req.setHeader('SOAPAction','"http://demo.smartpaperbpm.com/difc/soap_server/hellowsdl1.php#DoSomething"');
        stdemo = '';
        if (testFlag){    
          //Commented out By Mustafa Zaidi on 9th of July 2012...(because Amazon Webservice is down)
          /*HttpResponse res = h.send(req);
          xml = res.getBody();
          XmlStreamReader reader = res.getXmlStreamReader();
          
          stdemo = readXMLResponse(reader,'Pass');*/
          HttpResponse res = h.send(req);
          xml = res.getBody();
          XmlStreamReader reader = res.getXmlStreamReader();
          
          stdemo = readXMLResponse(reader,'Pass');
        }else{
          stdemo = 'www.difc.ae';
        }  
    }
    
    public static String readXMLResponse(XmlStreamReader reader, String sxmltag)
    {
        string retValue;
        // Read through the XML
        system.debug(reader.toString());
        while(reader.hasNext()) 
        {
            if (reader.getEventType() == XmlTag.START_ELEMENT) 
            {
                if (reader.getLocalName() == sxmltag) 
                {
                    reader.next();
                    if (reader.getEventType() == XmlTag.characters) 
                    {
                        retValue = reader.getText();    
                    }
                } 
            }
            reader.next();
        } 
        return retValue;
    }
}